# -*- coding: utf-8 -*-
import os
import shutil
from time import strftime

from PyQt4.QtCore import QString
from PyQt4.QtGui import QDialog, QMessageBox

try:
    _fromUtf8 = QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

from ui.uiShowPlotImage import ShowImage
from config.preferences import SRC_PATH


class ActionsSaveImage(ShowImage):

    def __init__(self, ui):
        """
            Construtor da classe.
        """
        self.ui = ui
        ShowImage.__init__(self, parent = None, origPixmap = None)



    def messageBoxImagemCorreta(self):
        """
        
        """
        self.previewImage()
        reply = QMessageBox.question(
                                     self,
                                     " TSM ",
                                     _fromUtf8(
                                     "Imagem está correta?"),
                                     QMessageBox.Yes | QMessageBox.No)
        if reply == QMessageBox.No:
            self.closeImage()
            return False
        elif reply == QMessageBox.Yes:
            self.closeImage()
            return True



    def messageBoxSalvarImagem(self):
        """
        
        """
        reply = QMessageBox.question(
                                     self,
                                     " TSM ",
                                     "Deseja Salvar Imagem?",
                                     QMessageBox.Yes | QMessageBox.No)
        if reply == QMessageBox.No:
            return False
        elif reply == QMessageBox.Yes:
            return True



    def salvarImagem(self):
        """
        
        """
        os.chdir(SRC_PATH)
        ordemProd = self.ui.lineEditOrdemProd.text()
        modeloAntena = str(self.nomeDoProduto())
        num = self.getNumSerie(modeloAntena)
        radical = str(num[0])
        numAntena = str(num[1])
        numeroSerie = radical + "-" + numAntena
        date = strftime("%Y%m%d")
        _path = SRC_PATH + "/tmp"
        os.chdir(_path)
        plotName = date + "_" + numeroSerie + "_" + ordemProd + ".jpg"
        shutil.move('plot.jpg', '%s' % plotName)
        shutil.move('%s' % plotName, _path + '/dados/%s' % modeloAntena)
        os.remove('plot.hp')
        QMessageBox.information(
                                None,
                                " TSM ",
                                "Imagem Salva!!!\n"
                                "Numero de serie: %s" % numeroSerie)
